/**
 * Created by user on 2015/9/22.
 */


var activityService = require('../../../services/mis/operate/activityService');
var messageUtil = require('../../../common/utils/messageUtil');
var toolUtil = require('../../../common/utils/toolUtil');
var nodeExcel = require('excel-export');

exports.activityList = function(req, res) {

    activityService.activityListService(req, function(err, results) {

        if (err) {
            logger.error('Exception happen when activityList...' + err.message);
            res.json(messageUtil.composeFailedMessage(req, 'Exception happen when activityList...'));
        }else {
            var data = {
                count:results[0]["count"],
                totalPage: results[0]["totalPage"],
                list: results[1]
            };
            res.json(messageUtil.composeSucceededMessage(req, null, data));
        }

    });

};

exports.activityAdd = function (req, res) {

    activityService.activityAddService(req, function (err, result) {
        if (err) {
            logger.error('Exception happened when addPrize ...!' + err.message);
            res.json(messageUtil.composeFailedMessage(req, err.message));
        } else {
            res.json(messageUtil.composeSucceededMessage(req, null, result));
        }
    });
};


exports.activityDelete = function (req, res) {

    activityService.activityDeleteService(req, function (err, result) {
        if (err) {
            logger.error('Exception happened when addPrize ...!' + err.message);
            res.json(messageUtil.composeFailedMessage(req, err.message));
        } else {
            res.json(messageUtil.composeSucceededMessage(req, null, result));
        }
    });
};


exports.activityEdit = function (req, res) {

    activityService.activityEditService(req, function (err, result) {
        if (err) {
            logger.error('Exception happened when editactivity ...!' + err.message);
            res.json(messageUtil.composeFailedMessage(req, err.message));
        } else {
            res.json(messageUtil.composeSucceededMessage(req, null, result));
        }
    });
};


exports.partakerList = function(req, res) {

    activityService.partakerListService(req, function(err, results) {

        if (err) {
            logger.error('Exception happen when partakerList...' + err.message);
            res.json(messageUtil.composeFailedMessage(req, 'Exception happen when partakerList...'));
        }else {
            var data = {
                count:results[0]["count"],
                totalPage: results[0]["totalPage"],
                list: results[1]
            };
            res.json(messageUtil.composeSucceededMessage(req, null, data));
        }

    });

};


exports.exportPartaker = function (req, res) {

    activityService.exportPartakerService(req, function (err, result) {
        if (err) {
            logger.error('Exception happened when exportJoinActivity...!' + err.message);
            res.json(messageUtil.composeFailedMessage(req, 'Exception happened when exportJoinActivity...!' + err.message));
        } else {
            var data = result[0];
            var conf = {};
            //logger.debug(result);
            conf.cols = [{
                caption: '小区',
                type: 'String'
            },
                {
                caption: '姓名',
                type: 'String'
            },
                {
                    caption: '电话',
                    type: 'String'
            },
                {
                caption: '备注',
                type: 'String'
            },
                {
                caption: '参加时间',
                type: 'String'
            }];

            var tempRows = new Array();
            for (var i = 0; i < data.length; i++) {

                //for (var j = 0, length = data[i].partake.length; j < length; j++) {

                    var temp = new Array();

                    var partake = data[i];
                    temp.push(partake ? data[i].community.communityName:"");
                    //temp.push(data[i].title ? data[i].title:"");

                    if (partake && partake._doc.user && partake._doc.user._doc.nickName) {
                        temp.push(partake._doc.user._doc.nickName);
                    }else {
                        temp.push("");
                    }

                    //temp.push(partake && partake.partakeType=="1"?"APP用户":"微信用户");
                    //temp.push(partake && partake.number ? partake.number+"" : "");
                    temp.push(partake && partake.phone ? partake.phone:"");
                    temp.push(partake && partake.desc ? partake.desc:"");

                    //temp.push(partake && partake.address ? partake.address:"");
                    temp.push(partake ? partake.dateCreated.getFullYear() + "-" + (partake.dateCreated.getMonth() + 1) + "-" + partake.dateCreated.getDate() + " " + partake.dateCreated.getHours() + ":" + partake.dateCreated.getMinutes():"");

                    tempRows.push(temp);
                //}

            }
            conf.rows = tempRows;

            var ret = nodeExcel.execute(conf);
            res.setHeader('Content-Type', 'application/vnd.openxmlformats');
            res.setHeader("Content-Disposition", "attachment; filename=" + "Report.xlsx");
            res.end(ret, 'binary');
        }
    })
};

exports.activityPush = function (req, res){

    activityService.activityPushService(req, function (err, result) {
        if (err) {
            logger.error('Exception happened when editactivity ...!' + err.message);
            res.json(messageUtil.composeFailedMessage(req, err.message));
        } else {
            res.json(messageUtil.composeSucceededMessage(req, null, {}));
        }
    });

};
